//
//  ViewController.m
//  HomeLearninig
//
//  Created by 李培根 on 2019/3/25.
//  Copyright © 2019 Bacon. All rights reserved.
//

#import "ViewController.h"
#import <Masonry.h>
#import <WHToast.h>
#define SafeAreaTopHeight (CGRectGetHeight(UIScreen.mainScreen.bounds) >= 812.0 ? 44 : 20)
NSString *const kDateKey = @"kDateKey";
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *plusLabel;
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIView *whiteNaView;
@property (weak, nonatomic) IBOutlet UITextField *tf;
@property (nonatomic, weak) NSUserDefaults *userDefault;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    _whiteNaView.backgroundColor = [UIColor whiteColor];
    _plusLabel.textColor = [UIColor colorWithRed:0.35 green:0.35 blue:0.35 alpha:1];
    _tf.placeholder = @"请输入时间，格式为YYYYMMDD";
    _tf.keyboardType = UIKeyboardTypeNumberPad;
    _tf.returnKeyType = UIReturnKeyDone;
    _userDefault = [NSUserDefaults standardUserDefaults];
    
    [_whiteNaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(SafeAreaTopHeight);
        make.height.mas_equalTo(64);
    }];
    
    [_btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.width.mas_equalTo(60);
        make.height.equalTo(self.whiteNaView.mas_height).multipliedBy(0.8);
        make.centerY.equalTo(self.whiteNaView);
    }];
    
    _tf.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    [_tf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteNaView).offset(15);
        make.right.equalTo(self.btn1.mas_left).offset(-15);
        make.centerY.equalTo(self.whiteNaView);
        make.height.equalTo(self.whiteNaView.mas_height).multipliedBy(0.7);
    }];
    
    [_plusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.whiteNaView.mas_bottom).offset(15);
        make.centerX.equalTo(self.whiteNaView);
    }];
    NSString *content = [_userDefault objectForKey:kDateKey];
    if (content) {
        _tf.text = content;
        [self calculateWithString:content];
    } else {
        [_tf becomeFirstResponder];
    }
}


- (IBAction)btn1Click:(id)sender {
    NSString *content = _tf.text;
    if (![self stringValidate:content]) {
        [WHToast showMessage:@"输入格式错误" duration:1 finishHandler:nil];
    } else {
        [_tf resignFirstResponder];
        [_userDefault setObject:_tf.text forKey:kDateKey];
        [_userDefault synchronize];
        [self calculateWithString:_tf.text];
    }
}

- (BOOL)stringValidate:(NSString *)string{
    NSString *regular = @"\\d{4}[0-1]\\d[0-3]\\d";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regular];
    BOOL isMatch = [pred evaluateWithObject:string];
    return isMatch;
}


- (void)calculateWithString:(NSString *)string {
    NSInteger days = [self getDifferenceByDate:string];
    NSString *dayStr = [NSString stringWithFormat:@"%@", @(days)];
    NSString *content = [NSString stringWithFormat:@"距离今天 %@ 天", dayStr];
    NSMutableAttributedString *muAttr = [[NSMutableAttributedString alloc] initWithString:content attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:0.35 green:0.35 blue:0.35 alpha:1], NSFontAttributeName: [UIFont systemFontOfSize:19]}];
    [muAttr addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:25]} range:[content rangeOfString:dayStr]];
    [muAttr addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:0.87 green:0.02 blue:0.13 alpha:1]} range:[content rangeOfString:dayStr]];
    _plusLabel.attributedText = muAttr;
}

- (NSInteger)getDifferenceByDate:(NSString *)date {
    //获得当前时间
    NSDate *now = [NSDate date];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    NSDate *newDate = [dateFormatter dateFromString:date];
    NSUInteger setting = (NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay);
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comNow = [calendar components:setting fromDate:now];
    NSDateComponents *comNew = [calendar components:setting fromDate:newDate];
    NSLog(@"%@\n%@", comNow, comNew);
    NSDateComponents *comps = [calendar components:(NSCalendarUnitHour) fromDate:now  toDate:newDate  options:0];
    NSLog(@"%@", @(comps.hour));
    NSInteger days = comps.hour / 24;
    NSInteger leftHour = comps.hour % 24;
    return (days + (leftHour == 0?0:1 ));
}


@end
